package com.atguigu.gmall.common.service;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.common.model.GmallCorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @Author atguigu-mqx
 * @Description TODO
 * @Date 2023/5/26 11:18
 * @Version 1.0
 */
@Service
public class RabbitService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 发送消息方法
     * @param exchange
     * @param routingKey
     * @param msg
     * @return
     */
    public Boolean sendMsg(String exchange,String routingKey, Object msg){
        //  创建对象
        GmallCorrelationData gmallCorrelationData = new GmallCorrelationData();
        String uuid = UUID.randomUUID().toString();
        gmallCorrelationData.setId(uuid);
        gmallCorrelationData.setExchange(exchange);
        gmallCorrelationData.setRoutingKey(routingKey);
        gmallCorrelationData.setMessage(msg);

        //  先设定重发的次数
        //  gmallCorrelationData.setRetryCount(6);

        //   将这个实体类：存储到redis;
        redisTemplate.opsForValue().set(uuid, JSON.toJSONString(gmallCorrelationData),10, TimeUnit.MINUTES);

        //  发送消息
        //  rabbitTemplate.convertAndSend(exchange,routingKey,msg);
        this.rabbitTemplate.convertAndSend(exchange,routingKey,msg,gmallCorrelationData);
        //  默认返回
        return true;
    }

    /**
     * 分组发生延迟消息方法
     * @param exchange
     * @param routingKey
     * @param msg
     * @param delayTime
     * @return
     */
    public Boolean sendDelayMsg(String exchange,String routingKey, Object msg, int delayTime){
        //  创建对象
        GmallCorrelationData gmallCorrelationData = new GmallCorrelationData();
        String uuid = UUID.randomUUID().toString();
        gmallCorrelationData.setId(uuid);
        gmallCorrelationData.setExchange(exchange);
        gmallCorrelationData.setRoutingKey(routingKey);
        gmallCorrelationData.setMessage(msg);
        //  表示延迟消息
        gmallCorrelationData.setDelay(true);
        //  设置延迟时间
        gmallCorrelationData.setDelayTime(delayTime);

        //  先设定重发的次数
        //  gmallCorrelationData.setRetryCount(6);

        //   将这个实体类：存储到redis;
        redisTemplate.opsForValue().set(uuid, JSON.toJSONString(gmallCorrelationData),10, TimeUnit.MINUTES);

        //  发送消息
        //  rabbitTemplate.convertAndSend(exchange,routingKey,msg);
        //  this.rabbitTemplate.convertAndSend(exchange,routingKey,msg,gmallCorrelationData);
        this.rabbitTemplate.convertAndSend(exchange,routingKey,msg,(message)-> {
            //  设置延迟时间
            message.getMessageProperties().setDelay(delayTime*1000);
            return message;
        },gmallCorrelationData);
        //  默认返回
        return true;
    }
}
